// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.asm.utils;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

import com.dustedpixels.common.AdapterSupplier;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class ClassAdapters {
  /**
   * Returns class adapter which would apply given method adapter to given method.
   * 
   * @param out class adapter output
   * @param methodName method name to process
   * @param methodDesc method descriptor process
   * @param methodAdapterSupplier supplier of method adapter
   * @return
   */
  public static ClassVisitor processMethod(
      ClassVisitor out,
      final String methodName, 
      final String methodDesc,
      final AdapterSupplier<MethodVisitor> methodAdapterSupplier) {
    
    return new ClassAdapter(out) {
      @Override
      public MethodVisitor visitMethod(int access, String name, String desc, String signature,
          String[] exceptions) {
        MethodVisitor out = super.visitMethod(access, name, desc, signature, exceptions);
        if (out != null && name.equals(methodName) && desc.equals(methodDesc)) {
          out = methodAdapterSupplier.get(out);
        }
        return out;
      }
    };
  }
}
